package com.goods.business.service.imp;

import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    //获取物资来源集合
    @Override
    public List<Supplier> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort desc");
        if (supplierVO.getAddress() != null && !"".equals(supplierVO.getAddress())){
            criteria.andLike("address","%" + supplierVO.getAddress() + "%");
        }
        if (supplierVO.getContact() != null && !"".equals(supplierVO.getContact())){
            criteria.andEqualTo("contact",supplierVO.getContact());
        }
        if (supplierVO.getName() != null && !"".equals(supplierVO.getName())){
            criteria.andLike("name","%" + supplierVO.getName() + "%");
        }
        List<Supplier> list = supplierMapper.selectByExample(example);

        List<SupplierVO> result = list.stream().map(supplier -> {
            SupplierVO s1 = new SupplierVO();
            BeanUtils.copyProperties(supplier, s1);
            return s1;
        }).collect(Collectors.toList());
        return list;
    }

    //打开编辑和表单的回显
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        Supplier supplier1 = supplierMapper.selectByPrimaryKey(supplier);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier1,supplierVO);
        return supplierVO;
    }

    //修改页面的保存
    @Override
    public void update(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //添加物资来源
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        Date date = new Date();
        supplier.setCreateTime(date);
        supplier.setModifiedTime(date);
        supplierMapper.insert(supplier);
    }

    //删除物资来源
    @Override
    public void delete(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplierMapper.deleteByPrimaryKey(supplier);
    }

    //加载来源数据
    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> list = supplierMapper.selectAll();
        List<SupplierVO> listVO = list.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return listVO;
    }


}
